#include <vector>
class Solution {
public:

    int n, m;
    int ret = 0;
    int dx[4] = { 0,-1,0,1 };
    int dy[4] = { 1,0,-1,0 };
    bool vis[201][201] = { 0 };
    int solve(vector<vector<char> >& grid) {

        n = grid.size();
        m = grid[0].size();

        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
                if (grid[i][j] == '1' && !vis[i][j])
                {
                    ret++;
                    dfs(grid, i, j);
                }
            }
        }
        return ret;
    }

    void dfs(vector<vector<char>>& grid, int i, int j)
    {
        vis[i][j] = true;
        for (int k = 0; k < 4; k++)
        {
            int a = dx[k] + i;
            int b = dy[k] + j;
            if (a >= 0 && a < n && b >= 0 && b < m && grid[a][b] == '1' && vis[a][b] == 0)
            {
                dfs(grid, a, b);
            }
        }
    }


};